package com.bootdo.repertory.controller;

import java.util.List;
import java.util.Map;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.repertory.domain.PorderDO;
import com.bootdo.repertory.service.PorderService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 进货订单
 * 
 * @author wang_wan_yu
 * @email 1992lcg@163.com
 * @date 2020-03-02 11:10:12
 */
 
@Controller
@RequestMapping("/repertory/porder")
public class PorderController {
	@Autowired
	private PorderService porderService;

	/*@Autowired
	private GoodsService goodsService;*/
	private String prefix = "repertory/porder";
	@GetMapping()
	@RequiresPermissions("repertory:porder:porder")
	String Porder(){
	    return "repertory/porder/porder";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("repertory:porder:porder")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<PorderDO> porderList = porderService.list(query);
		int total = porderService.count(query);
		PageUtils pageUtils = new PageUtils(porderList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("repertory:porder:add")
	String add(){
	    return "repertory/porder/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("repertory:porder:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		PorderDO porder = porderService.get(id);
		model.addAttribute("porder", porder);
		/*GoodsDO goods = goodsService.get(id);
		model.addAttribute("data", goods);*/
	    return "repertory/porder/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("repertory:porder:add")
	public R save( PorderDO porder){
		if(porderService.save(porder)>0){
			PorderDO porderDO = porderService.getInfo(porder);
			return R.ok(porderDO.getId()+"");
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("repertory:porder:edit")
	public R update( PorderDO porder/*,@RequestParam("ids") Integer id,GoodsDO goods*/){
		porderService.update(porder);
		/*goodsService.update(goods);*/
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("repertory:porder:remove")
	public R remove( Long id){
		if(porderService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("repertory:porder:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		porderService.batchRemove(ids);
		return R.ok();
	}


	//返回树状图页面
	@GetMapping("/addView")
	String insertView() {
		return  prefix + "/insertView";
	}

}
